package com.shinbada.modules.sys.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import com.shinbada.common.Common;
import com.shinbada.common.utils.Collections3;
import com.shinbada.config.swagger.IgnoreSwaggerParameter;
import com.shinbada.core.excel.options.UserState;
import com.shinbada.core.persistence.DataEntity;
import com.shinbada.excel.annotation.Excel;
import com.shinbada.excel.annotation.ExcelField;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.ToString;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

/**
 * 用户Entity
 *
 * @author initcap
 * @version 2016-12-05
 */
@Data
@ToString
@Excel("用户管理")
@ApiModel("用户实体")
public class User extends DataEntity<User> {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty("赋权管理域，以逗号分割 , ")
    private String domains;

    @ApiModelProperty("公司")
    private String dept;
    @ApiModelProperty("职务")
    private String job;

    @IgnoreSwaggerParameter
    @ApiModelProperty("归属公司")
    private Office company;

    @ApiModelProperty("岗位")
    private Post post;

    @ApiModelProperty("卡号")
    private String no;

    @ApiModelProperty("企业微信id")
    private String qiyeweixin;

    @ApiModelProperty("腕表标识")
    private String wanbiao;

    @IgnoreSwaggerParameter
    @ApiModelProperty("归属部门")
    @NotNull(message = "归属部门不能为空")
    @ExcelField(value = "部门", name = "office.levelName", width = 360)
    private Office office;

    @ExcelField(value = "登录名", width = 120)
    @ApiModelProperty("登录名")
    @Length(min = 1, max = 100, message = "登录名长度必须介于 1 和 100 之间")
    private String loginName;

    @ExcelField(value = "姓名", width = 120)
    @ApiModelProperty("姓名")
    @Length(min = 1, max = 100, message = "姓名长度必须介于 1 和 100 之间")
    private String name;

    @ExcelField(value = "邮箱", width = 120)
    @ApiModelProperty("邮箱")
    private String email;

    @ExcelField(
            value = "状态",
            readConverterExp = "禁用=0,启用=1",
            writeConverterExp = "0=禁用,1=启用",
            options = UserState.class
    )
    @ApiModelProperty("是否允许登陆")
    private String loginFlag;

    @ApiModelProperty("密码")
    @JsonIgnore
    @Length(min = 1, max = 100, message = "密码长度必须介于 1 和 100 之间")
    private String password;

    @ApiModelProperty("手机")
    private String mobile;

    @IgnoreSwaggerParameter
    @ApiModelProperty("最后登陆IP")
    private String loginIp;

    @IgnoreSwaggerParameter
    @ApiModelProperty("最后登陆日期")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date loginDate;

    @ApiModelProperty("头像")
    private String photo;

    @ApiModelProperty("原登录名")
    private String oldLoginName;

    @ApiModelProperty("新密码")
    private String newPassword;

    @ApiModelProperty("签名")
    private String sign;

    @IgnoreSwaggerParameter
    @ApiModelProperty("上次登陆IP")
    private String oldLoginIp;

    @IgnoreSwaggerParameter
    @ApiModelProperty("上次登陆日期")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date oldLoginDate;

    @ApiModelProperty("记录当前用户登录系统次数（1时需要强制修改密码，修改密码后重新登录）")
    private Integer loginTimes;

    @ApiModelProperty("密码变更时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date passwordChangeTime;

    @ApiModelProperty("密码连续输入错误次数（成功登录会重置0）")
    private Integer errInputPassword;

    @IgnoreSwaggerParameter
    @ApiModelProperty("根据角色查询用户条件")
    private Role role;

    @IgnoreSwaggerParameter
    @ApiModelProperty("拥有角色列表")
    @JsonIgnore
    private List<Role> roleList = Lists.newArrayList();

    private boolean supper = false;

    public User() {
        super();
    }

    public User(String id) {
        super(id);
    }

    public User(String id, String loginName) {
        super(id);
        this.loginName = loginName;
    }

    public User(Role role) {
        super();
        this.role = role;
    }

    public static boolean isAdmin(String id) {
        return Common.IS_ADMIN_USER_ID.equals(id) || Common.IS_SYSTEM_USER_ID.equals(id);
    }

    @Override
    public String getId() {
        return id;
    }

    @Override
    public String getRemarks() {
        return remarks;
    }

    @Override
    public Date getCreateDate() {
        return createDate;
    }

    public List<String> getRoleIdList() {
        List<String> roleIdList = Lists.newArrayList();
        for (Role role : roleList) {
            roleIdList.add(role.getId());
        }
        return roleIdList;
    }

    public void setRoleIdList(List<String> roleIdList) {
        roleList = Lists.newArrayList();
        for (String roleId : roleIdList) {
            Role role = new Role();
            role.setId(roleId);
            roleList.add(role);
        }
    }

    /**
     * 用户拥有的角色名称字符串, 多个角色名称用','分隔.
     */
    public String getRoleNames() {
        return Collections3.extractToString(roleList, "name", ",");
    }

    public String getRoleIds() {
        return Collections3.extractToString(roleList, "id", ",");
    }

    public boolean isAdmin() {
        return isAdmin(this.id);
    }
}
